/*This file is part of WZLibCP.

 *  WZLibCP is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

 *  WZLibCP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
    along with WZLibCP.  If not, see <http://www.gnu.org/licenses/>.
		
	Author: calc0000
*/
#include "stdafx.h"
#include "WZSubProperty.h"

#include "WZPrimitiveProperties.h"
#include "WZExtendedProperty.h"
#include "WZUOLProperty.h"

using namespace std;

namespace WZLib{
	WZObjectCollection&		WZSubProperty::properties(){return this->_properties;}
	ctor WZSubProperty::WZSubProperty(WZImage* parent,string name):WZImageProperty(parent,WZImageProperty::Prop_Sub,name,WZObject::Property){
	}
	dtor WZSubProperty::~WZSubProperty(){
		for(unsigned int i=0,l=this->_properties.size();i<l;i++)
			delete this->_properties[i];
		this->_properties.clear();
	}
	ErrorCode WZSubProperty::parse(WZBinaryReader* reader){
		return Error_WZLib_WZSubProperty_Parse_WrongParseFunction;
	}
	ErrorCode WZSubProperty::parse(WZBinaryReader* reader,unsigned int offset){//parses from when the prop list starts
		int entryCount=reader->ReadCompressedInt();
		for(unsigned int i=0;i<entryCount;i++){
			string name=reader->ReadStringBlock(offset);
			byte b=reader->ReadByte();
			WZImageProperty* prop=NULL;
			byte type=0;int eob=0;
			switch(b){
				case 0x00:
					prop=new WZNullProperty(this->_imgParent,name);
					break;
				case 0x0B:
				case 0x02:
					prop=new WZUnsignedShortProperty(reader->ReadUInt16(),this->_imgParent,name);
					break;
				case 0x03:
					prop=new WZCompressedIntProperty(reader->ReadCompressedInt(),this->_imgParent,name);
					break;
				case 0x04:
					type=reader->ReadByte();
					if(type==0x80)
						prop=new WZFloatProperty(reader->ReadSingle(),this->_imgParent,name);
					else if(type==0)
						prop=new WZFloatProperty(0.0,this->_imgParent,name);
					break;
				case 0x05:
					prop=new WZDoubleProperty(reader->ReadDouble(),this->_imgParent,name);
					break;
				case 0x08:
					prop=new WZStringProperty(reader->ReadStringBlock(offset),this->_imgParent,name);
					break;
				case 0x09:
					eob=(int)(reader->ReadUInt32()+reader->Position());
					prop=new WZExtendedProperty(this->_imgParent,name);
					((WZExtendedProperty*)prop)->parse(reader,offset,eob);
					if(reader->Position()!=eob)
						reader->Position(eob);
					WZExtendedProperty* exprop;exprop=(WZExtendedProperty*)prop;
					prop=((WZExtendedProperty*)prop)->prop();
					delete exprop;
					break;
				default:
					return Error_WZLib_WZSubProperty_Parse_UnknownPropertyType;
			}
			if(prop==NULL)
				continue;
			*(prop->parent())=this;
			this->_properties.push_back(prop);
		}
		this->_parsed=true;
		return Error_NoError;
	}
	void WZSubProperty::resolveUOLs(){
		for(unsigned int i=0,l=this->_properties.size();i<l;i++){
			WZImageProperty* p=(WZImageProperty*)this->_properties[i];
			if(p->propType()==WZImageProperty::Prop_UOL)
				this->_properties.setAt(i,((WZUOLProperty*)p)->resolve());
			delete p;
			p=(WZImageProperty*)this->_properties[i];
			if(((int)p->propType())/100==WZImageProperty::Prop_Sub)
				((WZSubProperty*)p)->resolveUOLs();
		}
	}
};